{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 1、利用os.walk(path)遍历目录树"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2023-07-02T05:52:22.777499800Z",
     "start_time": "2023-07-02T05:52:22.761501700Z"
    }
   },
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".\n",
      "[]\n",
      "['1_shutil模块.ipynb', '2_遍历目录树.ipynb', '3_zipfile模块压缩文件.ipynb']\n",
      "++++++++\n"
     ]
    }
   ],
   "source": [
    "for dirpath, subdirs, filenames in os.walk('.'):\n",
    "    print(dirpath)\n",
    "    print(subdirs)\n",
    "    print(filenames)\n",
    "    print(\"++++++++\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-07-02T05:53:25.452202Z",
     "start_time": "2023-07-02T05:53:25.438200200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../Test\n",
      "['b', 'demo']\n",
      "['List.py', 'test01.py', 'test02.py', 'test03.py', '这是测试文件.txt']\n",
      "++++++++\n",
      "../Test\\b\n",
      "['b']\n",
      "[]\n",
      "++++++++\n",
      "../Test\\b\\b\n",
      "[]\n",
      "[]\n",
      "++++++++\n",
      "../Test\\demo\n",
      "[]\n",
      "['01.py', '这是测试.txt']\n",
      "++++++++\n"
     ]
    }
   ],
   "source": [
    "for dirpath, subdirs, filenames in os.walk('../Test'):\n",
    "    print(dirpath)\n",
    "    print(subdirs)\n",
    "    print(filenames)\n",
    "    print(\"++++++++\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-07-02T03:54:21.482963Z",
     "start_time": "2023-07-02T03:54:21.416952700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "# os.walk(path)返回三元组，\n",
    "# 第一个是当前文件夹的路径\n",
    "# 第二个是当前文件夹下的所有直接子目录(列表形式)\n",
    "# 第三个是当前文件夹下的所有文件(列表形式)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-22T02:33:49.927031Z",
     "start_time": "2023-06-22T02:33:49.922205Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "# 实际运用\n",
    "# (1)第一个返回值可以用来拼接完整路径\n",
    "# (2)for循环是深度遍历当前文件夹及所有子文件夹\n",
    "# (3)核心是只需要处理第三个参数即可"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-07-02T03:58:20.422058100Z",
     "start_time": "2023-07-02T03:58:20.396023Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 2、利用Path对象的iterdir()方法"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "..\\Test\\demo\\01.py\n",
      "..\\Test\\demo\\这是测试.txt\n",
      "..\\Test\\List.py\n",
      "..\\Test\\test01.py\n",
      "..\\Test\\test02.py\n",
      "..\\Test\\test03.py\n",
      "..\\Test\\这是测试文件.txt\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "def traverse(path, handle_file_func):\n",
    "    \"\"\"\n",
    "    遍历目录树，并处理文件\n",
    "    :param path: 路径，str或Path对象\n",
    "    :param handle_file_func: 处理文件的函数\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    p = Path(path)\n",
    "    if p.is_file():\n",
    "        handle_file_func(p)\n",
    "        return\n",
    "    elif p.is_dir():\n",
    "        for p2 in p.iterdir():\n",
    "            traverse(p2, handle_file_func)\n",
    "\n",
    "def your_func(path):\n",
    "    print(path)\n",
    "\n",
    "\n",
    "traverse('../Test', your_func)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-07-02T06:03:22.609031Z",
     "start_time": "2023-07-02T06:03:22.518450200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### 注意: os.listdir(path)和iterdir(path)类似，只不过需要拼接路径，这很容易出错，不建议大家使用"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 3、同理os.walk(path)也可写成函数的形式"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../Test\\List.py\n",
      "../Test\\test01.py\n",
      "../Test\\test02.py\n",
      "../Test\\test03.py\n",
      "../Test\\这是测试文件.txt\n",
      "../Test\\demo\\01.py\n",
      "../Test\\demo\\这是测试.txt\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "def traverse2(path, handle_file_func):\n",
    "    \"\"\"\n",
    "    遍历目录树，并处理文件\n",
    "    :param path: 路径，str\n",
    "    :param handle_file_func: 处理文件的函数\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    for dirpath, subdirs, filenames in os.walk(path):\n",
    "        for filename in filenames:\n",
    "            filepath = os.path.join(dirpath, filename)\n",
    "            handle_file_func(filepath)\n",
    "\n",
    "def your_func(path):\n",
    "    print(path)\n",
    "\n",
    "traverse2('../Test', your_func)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-07-02T04:32:09.462114200Z",
     "start_time": "2023-07-02T04:32:09.407158800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
